import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/product_plateconsumption_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_move_line_odoo_api.dart';
import 'package:flutter_admin/models/odoo/product_plateconsumption.dart';
import 'package:flutter_admin/models/odoo/stock_move_line.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select_date.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import '../../../generated/l10n.dart';

class StockMoveLineEdit extends StatefulWidget {
  StockMoveLineEdit({this.stockMoveLine});

  final StockMoveLine? stockMoveLine;

  @override
  _StockMoveLineEditState createState() => _StockMoveLineEditState();
}

class _StockMoveLineEditState extends State<StockMoveLineEdit> {
  late StockMoveLine stockMoveLine;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;


  @override
  void initState() {
    super.initState();
    this.isAdd = widget.stockMoveLine == null;
    this.stockMoveLine = widget.stockMoveLine ?? StockMoveLine();
    _loadSelectVO();
  }

   _loadSelectVO() async{
    //activeList=[SelectOptionVO(value:'0',label: "禁用"),SelectOptionVO(value:'1',label: "启用")];
    setState(() { });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: 'picking_id',
            value: stockMoveLine.pickingId,
            onSaved: (v) {
              stockMoveLine.pickingId = v;
            },
          ),

          CryInput(
            label: 'move_id',
            value: stockMoveLine.moveId,
            onSaved: (v) {
              stockMoveLine.moveId = v;
            },
          ),

          CryInput(
            label: 'company_id',
            value: stockMoveLine.companyId,
            onSaved: (v) {
              stockMoveLine.companyId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'product_id',
            value: stockMoveLine.productId,
            onSaved: (v) {
              stockMoveLine.productId = v;
            },
          ),

          CryInput(
            label: 'product_uom_id',
            value: stockMoveLine.productUomId,
            onSaved: (v) {
              stockMoveLine.productUomId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'product_qty',
            value: stockMoveLine.productQty,
            onSaved: (v) {
              stockMoveLine.productQty = v;
            },
          ),

          CryInput(
            label: 'product_uom_qty',
            value: stockMoveLine.productUomQty,
            onSaved: (v) {
              stockMoveLine.productUomQty = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          // CryInput(
          //   label: 'qty_done',
          //   value: stockMoveLine.qtyDone,
          //   onSaved: (v) {
          //     stockMoveLine.qtyDone = v;
          //   },
          // ),

          CryInput(
            label: 'package_id',
            value: stockMoveLine.packageId,
            onSaved: (v) {
              stockMoveLine.packageId = v;
            },
          ),

          CryInput(
            label: 'package_level_id',
            value: stockMoveLine.packageLevelId,
            onSaved: (v) {
              stockMoveLine.packageLevelId = v;
            },
          ),

          CryInput(
            label: 'lot_id',
            value: stockMoveLine.lotId,
            onSaved: (v) {
              stockMoveLine.lotId = v;
            },
          ),

          CryInput(
            label: 'lot_name',
            value: stockMoveLine.lotName,
            onSaved: (v) {
              stockMoveLine.lotName = v;
            },
          ),

          CryInput(
            label: 'result_package_id',
            value: stockMoveLine.resultPackageId,
            onSaved: (v) {
              stockMoveLine.resultPackageId = v;
            },
          ),

           CrySelectDate(
            context,
            label: 'date',
            value: stockMoveLine.date,
            onSaved: (v) {
              stockMoveLine.date = v;
            },
          ),

          CryInput(
            label: 'owner_id',
            value: stockMoveLine.ownerId,
            onSaved: (v) {
              stockMoveLine.ownerId = v;
            },
          ),

          CryInput(
            label: 'location_id',
            value: stockMoveLine.locationId,
            onSaved: (v) {
              stockMoveLine.locationId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'location_dest_id',
            value: stockMoveLine.locationDestId,
            onSaved: (v) {
              stockMoveLine.locationDestId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),

          CryInput(
            label: 'state',
            value: stockMoveLine.state,
            onSaved: (v) {
              stockMoveLine.state = v;
            },
          ),

          CryInput(
            label: 'reference',
            value: stockMoveLine.reference,
            onSaved: (v) {
              stockMoveLine.reference = v;
            },
          ),

          CryInput(
            label: 'description_picking',
            value: stockMoveLine.descriptionPicking,
            onSaved: (v) {
              stockMoveLine.descriptionPicking = v;
            },
          ),

          CryInput(
            label: 'create_uid',
            value: stockMoveLine.createUid,
            onSaved: (v) {
              stockMoveLine.createUid = v;
            },
          ),

          CryInput(
            label: 'write_uid',
            value: stockMoveLine.writeUid,
            onSaved: (v) {
              stockMoveLine.writeUid = v;
            },
          ),

           CrySelectDate(
            context,
            label: 'write_date',
            value: stockMoveLine.writeDate,
            onSaved: (v) {
              stockMoveLine.writeDate = v;
            },
          ),

          CryInput(
            label: 'workorder_id',
            value: stockMoveLine.workorderId,
            onSaved: (v) {
              stockMoveLine.workorderId = v;
            },
          ),

          CryInput(
            label: 'production_id',
            value: stockMoveLine.productionId,
            onSaved: (v) {
              stockMoveLine.productionId = v;
            },
          ),

          CryInput(
            label: 'job_type',
            value: stockMoveLine.jobType,
            onSaved: (v) {
              stockMoveLine.jobType = v;
            },
          ),

          CryInput(
            label: 'job_json',
            value: stockMoveLine.jobJson,
            onSaved: (v) {
              stockMoveLine.jobJson = v;
            },
          ),

          CryInput(
            label: 'in_check',
            value: stockMoveLine.inCheck,
            onSaved: (v) {
              stockMoveLine.inCheck = v;
            },
          ),

          CryInput(
            label: 'out_check',
            value: stockMoveLine.outCheck,
            onSaved: (v) {
              stockMoveLine.outCheck = v;
            },
          ),

        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            StockMoveLineOdooApi.saveOrUpdate(stockMoveLine.toOdooMap()).then((ResponseBodyApi res) {
              if (!res.success!) {
               //package:flutter_admin/pages/common/cry(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}